using System;
using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
namespace Framework.Fsm
{
    public class FsmSearchActionProvider : ScriptableObject, ISearchWindowProvider
    {
        internal class SelectItem
        {
            public Type Type;
            public string Title;
            public int Level;
        }
        private FsmGraphContext _context;
        internal List<SelectItem> _nodes = new List<SelectItem>();
        public Action<Type, string> OnSelect;
        private bool _dirty;
        public void Initialize(FsmGraphContext context)
        {
            _context = context;
            _dirty = true;
        }
        private void init()
        {
            if (!_dirty) return;
            _dirty = false;
            if (_context.ActionType == null) return;
            _nodes.Clear();
            var titlePaths = new HashSet<string>();
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (var type in assembly.GetTypes())
                {
                    if (type.IsClass && !type.IsAbstract && (type.IsSubclassOf(_context.ActionType)))
                    {
                        FsmActionItemAttribute[] attrs = type.GetCustomAttributes(typeof(FsmActionItemAttribute), false) as FsmActionItemAttribute[];
                        if (attrs == null || attrs.Length < 1 || string.IsNullOrEmpty(attrs[0].Title))
                        {
                            SelectItem sitem = new SelectItem { Type = type, Title = type.Name, Level = 1 };
                            _nodes.Add(sitem);
                        }
                        else
                        {
                            foreach (var item in attrs)
                            {
                                var pathLevel = 0;
                                var nodeName = item.Title;
                                var parts = nodeName.Split('/');
                                if (parts.Length > 1)
                                {
                                    pathLevel++;
                                    nodeName = parts[parts.Length - 1];
                                    var fullTitleAsPath = "";

                                    for (var i = 0; i < parts.Length - 1; i++)
                                    {
                                        var title = parts[i];
                                        fullTitleAsPath += title;
                                        pathLevel = i + 1;

                                        // Add section title if the node is in subcategory
                                        if (!titlePaths.Contains(fullTitleAsPath))
                                        {
                                            _nodes.Add(new SelectItem { Type = null, Title = title, Level = pathLevel });
                                            titlePaths.Add(fullTitleAsPath);
                                        }
                                    }
                                }
                                _nodes.Add(new SelectItem { Type = type, Title = nodeName, Level = pathLevel + 1 });
                            }
                        }
                    }
                }
            }
        }
        public List<SearchTreeEntry> CreateSearchTree(SearchWindowContext context)
        {
            init();
            var entries = new List<SearchTreeEntry>
            {
                new SearchTreeGroupEntry(new GUIContent("选择行为"))
            };
            foreach (var item in _nodes)
            {
                if (item.Type == null)
                {
                    entries.Add(new SearchTreeGroupEntry(new GUIContent(item.Title)) { level = item.Level, userData = item });
                }
                else
                {
                    entries.Add(new SearchTreeEntry(new GUIContent(item.Title)) { level = item.Level, userData = item });
                }

            }
            return entries;
        }

        public bool OnSelectEntry(SearchTreeEntry searchTreeEntry, SearchWindowContext context)
        {
            var item = searchTreeEntry.userData as SelectItem;
            // var action = new BaseActionNodeView();
            // _context.Title = item.Title;
            // action.Initialize(_context);
            // _context.From.AddAction(action);
            if (OnSelect != null)
            {
                OnSelect.Invoke(item.Type, item.Title);
            }
            return true;

        }

    }
}